package com.tmzn.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tmzn.domain.TDevicePlan;
import com.tmzn.domain.TDevicePlanAddNewDTO;
import com.tmzn.entity.AjaxResult;
import com.tmzn.jwt.JwtKit;
import com.tmzn.jwt.bean.MyIJwtAble;
import com.tmzn.service.ITDevicePlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/plan")
public class TDevicePlanController{

    @Autowired
    private ITDevicePlanService tDevicePlanService;

    // http://localhost:8080/add-new
    @PostMapping("/add")
    public AjaxResult addNew(@RequestBody JSONObject params) {
        log.info("开始处理【添加预约内容】的请求，参数：{}", params);
        log.info(String.valueOf(params));
        String resultJsonString = JSON.toJSONString(params);
        JSONObject data = JSON.parseObject(resultJsonString);
        JSONObject realData = data.getJSONObject("params");
        TDevicePlanAddNewDTO tDevicePlanAddNewDTO=new TDevicePlanAddNewDTO();
// 将 params 字段中的各个子字段进行赋值
        String createTime = String.valueOf(LocalDateTime.now());
        long planType = realData.getLongValue("planType");
        long status = realData.getLongValue("status");
        Integer port = realData.getInteger("port");
        MyIJwtAble loginUser = JwtKit.getLoginUser();
        Integer userId = loginUser.getId();
        String runTime = realData.getString("runTime");
        String ccid = realData.getString("ccid");
        LocalDateTime parsedRunTime = null; // 初始化为null
        if (runTime != null && !runTime.isEmpty()) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
            parsedRunTime = LocalDateTime.parse(runTime, formatter);
        }
        String repeatDays = realData.getString("repeatDays");
        String repeatTime = realData.getString("repeatTime");
        String deviceId = realData.getString("deviceId");
        tDevicePlanAddNewDTO.setCreateTime(LocalDateTime.parse(createTime));
        tDevicePlanAddNewDTO.setPlanType(planType);
        tDevicePlanAddNewDTO.setStatus(status);
        tDevicePlanAddNewDTO.setUserId(userId);
        tDevicePlanAddNewDTO.setRunTime(parsedRunTime);
        tDevicePlanAddNewDTO.setRepeatDays(repeatDays);
        tDevicePlanAddNewDTO.setRepeatTime(repeatTime);
        tDevicePlanAddNewDTO.setDeviceId(deviceId);
        tDevicePlanAddNewDTO.setCcid(ccid);
        tDevicePlanAddNewDTO.setPort(port);
//        TDevicePlanAddNewDTO tDevicePlanAddNewDTO=new TDevicePlanAddNewDTO();
//        BeanUtils.copyProperties(params, tDevicePlanAddNewDTO);

        tDevicePlanService.addNew(tDevicePlanAddNewDTO);
        return AjaxResult.success();
    }

    @PostMapping("/list")
    public AjaxResult planList(){
//        startPage();
//        LambdaQueryWrapper<TDevicePlan> objectLambdaQueryWrapper = Wrappers.lambdaQuery();
//        objectLambdaQueryWrapper.orderByDesc(TDevicePlan::getCreateTime);
//        LoginUser loginUser = getLoginUser();
//        Long userId = loginUser.getUserId();
//        objectLambdaQueryWrapper.eq(TDevicePlan::getUserId,userId);
//        List<TDevicePlan> list = tDevicePlanService.list(objectLambdaQueryWrapper);
//        return AjaxResult.success(list);
        return null;
    }

}
